<?php
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
//$opUI = Blyn::app()->getActiveOperation()->getUIManager();
$url = $screen->getAjaxLinkUrl('saveEvent', NULL, array('eventId' => $eventId, 'postModel' => $modelClass));
//$url = $opUI->getAjaxLinkUrl('saveEvent', NULL, array('eventId' => $eventId,'postModel'=> $modelClass));
$updateId = $screen->getAjaxContainerId();
//$updateId = $opUI->getAjaxContainerId();
//$screen->addContextVariable('postModel', $modelClass);
?>
<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>

<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send<?php echo $eventId; ?>() {

        var formData = new FormData($("#post-form-2")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            //datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>

<?php
$app = Blyn::app()->getActiveApp();
$typeList = $app->getEventTypeList();
$list = array('0' => '请选择');
$typeList = $list + $typeList;
/**
 * 定义field的名字的时候，一定按照以下规范来定义：
 * 1）下划线作为分隔符
 * 2）第一部分是类型，中间是class，然后是class里面的属性的名字，有时候还可以加一个后缀
 */
$eventTypeField = 'eventType_eventTypeId_' . $eventId;
$eventNameField = 'event_name_' . $eventId;
$startTimeField = 'event_startTime_' . $eventId;
$endTimeField = 'event_endTime_' . $eventId;
$descriptionField = 'event_description_' . $eventId;
$imageField = 'image_media_image_' . $eventId;
$model->addField($eventNameField);
$model->addField($startTimeField);
$model->addField($endTimeField);
$model->addField($descriptionField);
$model->addField($imageField);
$model->addField($eventTypeField);

if ($eventId > 0) {
    $event = new BWEvent($eventId);
    $eventType = $event->getGroup(array('parent' => BWEventGroup::Event_TYPE_Promotion));
    if ($eventType)
        $model->$eventTypeField = $eventType->getId();
    $model->$eventNameField = $event->name;
    $model->$startTimeField = $event->startTime;
    $model->$endTimeField = $event->endTime;
    $model->$descriptionField = $event->description;
    $media = $event->getMediaManager()->getMedia('image');
    if ($media)
        $model->$imageField = $event->getMediaManager()->getMedia('image')->content;
}
?>
<div id="myModal<?php echo $eventId; ?>" class="modal hide fade" style="width: 1000px;left: 33%; " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3>微活动添加</h3>
    </div>
    <div class="modal-body" id="Event_Add_Body" style="display:block">
        <table class="table table-bordered table-striped">
            <colgroup>
                <col class="span1">
                <col class="span7">
            </colgroup>
            <thead>
                <tr>
                    <th>活动属性</th>
                    <th>详细内容</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <code>活动类型</code>
                    </td>
                    <td>
                        <?php
                        echo $form->dropDownList($model, $eventTypeField, $typeList, array('class' => 'span7'));
                        ?>
                        <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
                    </td>
                </tr>
                <tr><td> <code>活动名称</code> </td>
                    <td>
                        <?php
                        echo $form->textField($model, $eventNameField, array('placeholder' => '活动名称...', 'required' => true));
                        ?>
                    </td>
                </tr>                
                <tr><td> <code>活动图片</code> </td>
                    <td>
                        <?php
                        echo $form->fileField($model, $imageField); // image file select when clicks on upload photo
                        if ($eventId > 0) {
                            $imageUrl = Yii::app()->request->baseUrl . $model->$imageField;
                            echo "<img src=" . $imageUrl . " width=50 height=50 />";
                        }
                        ?>

                    </td>
                </tr>
            <script type="text/javascript">
                $(function() {
                    $('#datetimepicker1<?php echo $eventId ?>').datetimepicker({
                        language: 'pt-BR'
                    });
                    $('#datetimepicker2<?php echo $eventId ?>').datetimepicker({
                        language: 'pt-BR'
                    });
                });</script>
            <tr> <td> <code>活动开始时间</code> </td>
                <td>
                    <div class="input-append" id="datetimepicker1<?php echo $eventId ?>">
                        <?php
                        echo $form->textField($model, $startTimeField, array('data-format' => "yyyy-MM-dd hh:mm:ss", 'type' => "text", 'placeholder' => '开始时间...'));
                        ?>
                        <span class="add-on">
                            <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                            </i>
                        </span>
                    </div> 
                </td>
            </tr>
            <tr><td> <code>活动结束时间</code></td>
                <td>
                    <div class="input-append" id="datetimepicker2<?php echo $eventId ?>">
                        <?php
                        echo $form->textField($model, $endTimeField, array('data-format' => "yyyy-MM-dd hh:mm:ss", 'type' => "text", 'placeholder' => '结束时间...'));
                        ?>
                        <span class="add-on">
                            <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                            </i>
                        </span>
                    </div> 
                </td>
            </tr>
            <tr>
                <td> <code>活动简介</code>  </td>                    
                <td>
                    <?php
                    echo $form->textArea($model, $descriptionField, array('placeholder' => '活动简介...'));
                    ?>
            </tr>
            </tbody>
        </table>  
    </div>

    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
        <button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:send<?php echo $eventId; ?>()">保存</button>
    </div>
</div>

